$(function() {
	$('.ranks').hide();
//	var h1 = $('#viewed').height();
//	var h2 = $('#voted').height();
//	$('.ranks').height( Math.max(h1,h2) );
    $('#viewed').show();
    $('select').change(function() {   
        var val = $(this).val();
        if (val == "viewed") {
            $('#voted').slideUp();
//            $('#viewed').slideDown();
        }
        if (val == "voted") {
            $('#viewed').slideUp();
//            $('#voted').slideDown();
          }
        $('#' + val).slideDown();
    });
});


function topUpdateInit() {
	$.getJSON("/writeonproject/update?page=top_viewed", topViewedCallBack);
	$.getJSON("/writeonproject/update?page=top_voted", topVotedCallBack);
}

function topViewedCallBack(data) {
	if( data != null && data.error != null && data.error == "false" ) {
		var storybody = document.getElementById('top_viewed');
		if( data.stories.length > 0 ) {
			for( var i = data.stories.length-1; i >= 0; i-- ) {
				var story = data.stories[i];
				var bottomRow = storybody.insertRow ( 0 );
				var topRow = storybody.insertRow( 0 );
				
				topRow.className = "rowon story1";
				var topcell1 = topRow.insertCell( 0 );
				var topcell2 = topRow.insertCell( 1 );
				var topcell3 = topRow.insertCell( 2 );
				var topcell4 = topRow.insertCell( 3 );
				var topcell5 = topRow.insertCell( 4 );
				
				topcell1.className = "num center_col";
				topcell1.innerHTML = (i+1);
				topcell2.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
				topcell3.className = "small_date";
				topcell3.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
				topcell4.className = "story_post";
				topcell4.id = "pcount" + story.storyid;
				topcell4.innerHTML = story.numpost;
				topcell5.className = "small_date";
				topcell5.id = "time" + story.storyid;
				topcell5.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
				
				bottomRow.className = "rowon";
				var bottom1 = bottomRow.insertCell( 0 );
				bottom1.className = "story_preview";
				bottom1.id = "sp" + story.storyid;
				bottom1.colSpan = "5";
				bottom1.innerHTML = story.preview;
			}
		}
	}
}

function topVotedCallBack(data) {
	if( data != null && data.error != null && data.error == "false" ) {
		var storybody = document.getElementById('top_voted');
		if( data.stories.length > 0 ) {
			for( var i = data.stories.length-1; i >= 0; i-- ) {
				var story = data.stories[i];
				var bottomRow = storybody.insertRow ( 0 );
				var topRow = storybody.insertRow( 0 );
				
				topRow.className = "rowon story1";
				var topcell1 = topRow.insertCell( 0 );
				var topcell2 = topRow.insertCell( 1 );
				var topcell3 = topRow.insertCell( 2 );
				var topcell4 = topRow.insertCell( 3 );
				var topcell5 = topRow.insertCell( 4 );
				
				topcell1.className = "num center_col";
				topcell1.innerHTML = (i+1);
				topcell2.innerHTML = "<a href=\"/writeonproject/home?page=story_view&storyid=" + story.storyid + "\" class=\"title\">" + story.storytitle + "</a>";
				topcell3.className = "small_date";
				topcell3.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.authorid + "\" class=\"author\">" + story.authorid + "</a><br>" + story.createdate;
				topcell4.className = "story_post";
				topcell4.id = "pcount" + story.storyid;
				topcell4.innerHTML = story.numpost;
				topcell5.className = "small_date";
				topcell5.id = "time" + story.storyid;
				topcell5.innerHTML = "<a href=\"/writeonproject/home?page=profile&userid=" + story.lastposterid + "\" class=\"author\">" + story.lastposterid + "</a><br>" + story.timestamp;
				
				bottomRow.className = "rowon";
				var bottom1 = bottomRow.insertCell( 0 );
				bottom1.className = "story_preview";
				bottom1.id = "sp" + story.storyid;
				bottom1.colSpan = "5";
				bottom1.innerHTML = story.preview;
			}
		}
	}
}